import input from '@/utils/inputCheck';
export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      registerForm: {
        phone: '',
        email: '',
        password: '',
        rePassword: '',
        captcha: ''
      },
      logCapForm: {
        phone: '',
        captcha: ''
      },
    };
  },
  methods: {
    goBackLogic(arr = []) {
      arr.push(this.$route.path);
      let routeList = this.$store.state.routeList;
      let rouLen = routeList.length;
      if (!rouLen) return this.go(-1);
      for (let i = 0; i < rouLen; i++) {
        let route = routeList[rouLen - i - 1];
        // 如果黑名单列表包含当前路由且为当前页面
        let flag = [];
        for (let j = 0; j < arr.length; j++) {
          if(route.indexOf(arr[j]) === -1) flag.push(true);
          else flag.push(false);
        }
        flag = Array.from(new Set(flag));
        if(flag.length === 1 && flag[0]) return this.go(route);
      }
      this.go(-1);
    },
    /**
     * login
     */
    async login_submitForm() {
      try {
        const res = await this.$post({
          url: '/rest/V1/customer/loginByPassword',
          data: this.loginForm
        });
        localStorage.setItem("token", res[0].token);
        this.$store.commit('update', {
          personInfo: res[0]
        });
        this.goBackNoLogin();
        // this.go(`/my`);
      } catch (e) {
        this.$$error(e.message);
      }
    },
    // 点击提交按钮
    login_confirmForm() {
      // 检测表单是否合法
      const res = this.login_checkForm();
      if (res !== -1) {
        // 如果验证不通过
        this.$$warning(input.checkFormErrorMsg(res));
      } else {
        console.log("验证通过");
        this.login_submitForm();
      }
    },
    login_checkForm() {
      const phone = input.checkPhone(this.loginForm.username);
      const email = input.checkEmail(this.loginForm.username);
      if (phone !== -1 && email !== -1) return 60;
      if (this.loginForm.password === "") return 41;
      else return -1;
    },
    /**
     * register
     */
    
    async reg_submitForm() {
      try {
        const res = await this.$post({
          url: '/rest/V1/customer/register',
          data: {
            mobile: this.registerForm.phone,
            email: this.registerForm.email,
            password: this.registerForm.password,
            sms_code: this.registerForm.captcha,
            firstname: this.registerForm.phone
          }
        });
        localStorage.setItem("token", res[0].token);
        this.$store.commit('update', {
          personInfo: res[0]
        });
        this.$$success("您已注册成功！");
        this.goBackNoLogin();
        // this.go(`/my`);
      } catch (e) {
        this.$$error(e.message);
      }
    },
    // 点击提交按钮
    reg_confirmForm() {
      // 检测表单是否合法
      const res = this.reg_checkForm();
      if (res !== -1) {
        // 如果验证不通过
        this.$toast(input.checkFormErrorMsg(res));
      } else {
        this.reg_submitForm();
      }
    },
    reg_checkForm() {
      // 手机号格式
      const resPhone = input.checkPhone(this.registerForm.phone);
      if (resPhone !== -1) return resPhone;
      // 图形验证码
      const resImgCheck = input.checkImg(this.imgCheckCode);
      if (resImgCheck !== -1) return resImgCheck;
      // 验证码
      const resCaptcha = this.registerForm.captcha;
      if (!resCaptcha) return 20;
      // 邮箱格式
      const resEmail = input.checkEmail(this.registerForm.email);
      if (resEmail !== -1) return resEmail;
      // 密码格式 8-16位数字字母下划线
      const rePassword = input.checkPassword(this.registerForm.password);
      if (rePassword !== -1) return rePassword;
      // 确认密码
      const resRePassword = input.checkRePassword(this.registerForm.password, this.registerForm.rePassword);
      if (resRePassword !== -1) return resRePassword;
      else return -1;
    },
    /**
     * reset
    */
    async reset_submitForm() {
      try {
        await this.$post({
          url: '/rest/V1/customer/forgotPassword',
          data: {
            mobile: this.resetForm.phone,
            password: this.resetForm.password,
            sms_code: this.resetForm.captcha
          }
        });
        this.$toast("密码修改成功");
        this.step = 3;
      } catch (e) {
        this.$$error(e.message);
      }
    },
    
    // 点击提交按钮
    reset_confirmForm() {
      // 检测表单是否合法
      const res = this.reset_checkForm();
      if (res !== -1) {
        // 如果验证不通过
        this.$toast(input.checkFormErrorMsg(res));
      } else {
        console.log("验证通过");``;
        this.reset_submitForm();
      }
    },
    reset_checkForm() {
      // 手机号格式
      const resPhone = input.checkPhone(this.resetForm.phone);
      if (resPhone !== -1) return resPhone;
      // 验证码
      const resCaptcha = this.resetForm.captcha;
      if (!resCaptcha) return 20;
     
      // 密码格式 8-16位数字字母下划线
      const rePassword = input.checkPassword(this.resetForm.password);
      if (rePassword !== -1) return rePassword;
      // 确认密码
      const resRePassword = input.checkRePassword(this.resetForm.password, this.resetForm.rePassword);
      if (resRePassword !== -1) return resRePassword;
      else return -1;
    },
    /**
     * loginSSM
     */
    logCap_sendCaptcha() {
      this.sendCaptcha(this.logCapForm.phone);
    },
    async logCap_submitForm() {
      try {
        const res = await this.$post({
          url: '/rest/V1/customer/loginBySms',
          data: {
            mobile: this.logCapForm.phone,
            sms_code: this.logCapForm.captcha
          }
        });
        localStorage.setItem("token", res[0].token);
        this.$store.commit('update', {
          personInfo: res[0]
        });
        this.goBackNoLogin();
        // this.go(`/my`);
        // 如果提交成功
      } catch (e) {
        this.$$error(e.message);
      }
    },
    // 点击提交按钮
    logCap_confirmForm() {
      // 检测表单是否合法
      const res = this.logCap_checkForm();
      if (res !== -1) {
        // 如果验证不通过
        this.$toast(input.checkFormErrorMsg(res));
      } else {
        console.log("验证通过");
        this.logCap_submitForm();
      }
    },
    logCap_checkForm() {
      // 手机号格式
      const resPhone = input.checkPhone(this.logCapForm.phone);
      if (resPhone !== -1) return resPhone;
      // 图形验证码
      const resImgCheck = input.checkImg(this.imgCheckCode);
      if (resImgCheck !== -1) return resImgCheck;
      if (!this.logCapForm.captcha) return 20;
      else return -1;
    },
    async setShareRelation(){
      const storageTrackId = localStorage.getItem("track_id");
      if(storageTrackId){
        try {  
          await this.$post({
            url: "/rest/V1/cps/mine/record",
            data:{
              "track_id" : storageTrackId
            }
          });
          localStorage.setItem("track_id", '');
        } catch (e) {
          localStorage.setItem("track_id", storageTrackId);
        }
      }
    }
  }
};